package com.kinghood.authoritycenter.manager.impl;

import com.kinghood.authoritycenter.common.model.constant.AuthConstant;
import com.kinghood.authoritycenter.common.model.dao.auth.OpenAccountDO;
import com.kinghood.authoritycenter.common.model.dao.auth.UserBaseDO;
import com.kinghood.authoritycenter.common.model.dto.AddOpenReqDTO;
import com.kinghood.authoritycenter.common.model.dto.auth.LoginDTO;
import com.kinghood.authoritycenter.common.model.dto.auth.RegisterReqDTO;
import com.kinghood.authoritycenter.common.util.UserUtil;
import com.kinghood.authoritycenter.manager.IOauthManager;
import com.kinghood.authoritycenter.service.IOpenAccountService;
import com.kinghood.authoritycenter.service.IRedisService;
import com.kinghood.authoritycenter.service.IUserBaseService;
import com.kinghood.authoritycenter.service.LoginService;
import com.kinghood.infrastructure.model.KingHoodExceptionUtil;
import com.kinghood.authoritycenter.common.model.dto.authority.AuthByUserDTO;
import com.kinghood.authoritycenter.common.model.dto.authority.TokenReqDTO;
import com.kinghood.authoritycenter.common.model.dto.auth.KingHoodPrincipal;
import com.kinghood.authoritycenter.common.model.dto.auth.UserDTO;
import com.kinghood.redis.util.RedisManager;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Objects;

/**
 * description
 *
 * @author zhangFanJun
 * @date 2023-12-06 15:12
 **/
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
@Slf4j
@Service
public class IOauthManagerImpl implements IOauthManager {

    private final LoginService loginService;
    private final IUserBaseService userBaseService;
    private final IOpenAccountService openAccountService;
    private final RedisManager redisManager;
    private final IRedisService redisService;

    @Override
    public UserBaseDO register(RegisterReqDTO in) {
        return userBaseService.register(in);
    }

    @Override
    public KingHoodPrincipal<UserDTO> login(LoginDTO in) {
        // 校验用户
        UserDTO userDTO = userBaseService.checkLogin(in.getUsername(), in.getPassword());
        // 创建token
        KingHoodPrincipal<UserDTO> principal = loginService.login(userDTO);

        return principal;
    }


    @Override
    public Boolean logout(String bear) {
        if (!StringUtils.hasText(bear) || !bear.startsWith("Bearer ")) {
            KingHoodExceptionUtil.throwException("不存在token");
        }
        UserDTO userDetail = UserUtil.getUser();
        KingHoodExceptionUtil.isNull(userDetail, "用户信息异常");
        String token = bear.substring(7);

        redisService.logout(token,Objects.requireNonNull(userDetail.getId()).toString(),Objects.requireNonNull(UserUtil.get()).getExpire());
        return true;
    }

    @Override
    public KingHoodPrincipal<OpenAccountDO> openToken(TokenReqDTO in) {
        // 校验用户
        OpenAccountDO openAccountDO = openAccountService.checkLogin(in.getAppId(), in.getAppSecret());

        // 创建token
        return loginService.openToken(openAccountDO);
    }

    @Override
    public Boolean addOpen(AddOpenReqDTO in) {

        return openAccountService.addOpen(in);
    }

    @Override
    public AuthByUserDTO getAuth(String issuer, Long id) {

        switch (issuer){
            case AuthConstant.ISSUER_INNER:
                return userBaseService.getAuth(id);
            case AuthConstant.ISSUER_OPEN:

                break;
            default:
        }
        return null;
    }
}
